// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

const db = cloud.database()

// 云函数入口函数
exports.main = async event => {
  const { OPENID: _openid } = cloud.getWXContext()
  if (!_openid) {
    throw new Error('no openid')
  }

  const {
    data: [uData],
  } = await db.collection('store').where({ _openid }).get()
  if (!uData) {
    throw new Error('no login store')
  }

  const { description, address, location, phoneNumber } = event
  const data = {
    description,
    address,
    location: location
      ? db.Geo.Point(location.longitude, location.latitude)
      : undefined,
    phoneNumber,
  }

  await db.collection('store').where({ _openid }).update({ data })
}
